package com.yunmai.steplibrary.utils;
import android.os.Environment;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 日志打印
 * <p>
 * 功能详细描述
 *
 * @author Owen Xie
 * @version [yunmai, 2015-10-8]
 */
public abstract class Log {
    private static final String TAG = "Log";

    /**
     * 日期格式
     */
    private static SimpleDateFormat dateFormat =
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);
    /**
     * 保存文件路径
     */
    public static String logPath = "/yunmai/Log";

    /**
     * 保存文件路径
     */
    public static String logSwitchPath = "/yunmai/setting.xml";

    /**
     * Log写入文件的文件名
     */
    private static final String LogName = "android.log";

    /**
     * verbose 级别 logcat 日志预设变量(常规log打印，开发人员自己修改控制-默认不输出)
     */
    private static boolean isPrintVerboseLogPreset = true;
    /**
     * 打印verbose日志到SDCard文件的预设变量
     */
    private static boolean isPrintVerboseLogSDPreset = false;

    /**
     * info 级别 logcat 日志预设变量(常规log打印，开发人员自己修改控制-默认不输出)
     */
    private static boolean isPrintInfoLogPreset = true;
    /**
     * 打印info日志到SDCard文件的预设变量
     */
    private static boolean isPrintInfoLogSDPreset = false;

    /**
     * warn 级别 logcat 日志预设变量(常规log打印，开发人员自己修改控制-默认不输出)
     */
    private static boolean isPrintWarnLogPreset = true;
    /**
     * 打印warn日志到SDCard文件的预设变量
     */
    private static boolean isPrintWarnLogSDPreset = false;

    /**
     * error 级别 logcat 日志预设变量(常规log打印，开发人员自己修改控制-默认不输出)
     */
    private static boolean isPrintErrorLogPreset = true;
    /**
     * 打印error日志到SDCard文件的预设变量
     */
    private static boolean isPrintErrorLogSDPreset = true;

    /**
     * debug 级别 logcat日志预设变量(常规log打印，开发人员自己修改控制-默认不输出)-
     */
    private static boolean isPrintDebugLogPreset = true;


    /**
     * 打印debug日志到SDCard文件的预设变量
     */
    private static boolean isPrintDebugLogSDPreset = true;
    private static String LogName1 = "androidstep.log";

    /**
     * 打印verbose级别的日记
     *
     * @param tag  标记
     * @param text 日志内容
     */
    public static void verbose(String tag, String text) {

        if (isPrintVerboseLogPreset) {
            android.util.Log.v(tag, " " + text);
        }

        if (isPrintVerboseLogSDPreset) {
            storeLog("v", tag, text);
        }
    }

    public static void verbose(String tag, String format, Object... args) {

        String text = format;
        if (args.length > 0)
            text = String.format(format, args);
        verbose(tag, text);
    }

    public static boolean logFilter(String tag) {
        if (tag != null
            && (tag.contains("step"))) {
            return true;
        }
        return false;
    }

    /**
     * 打印debug级别的日记
     *
     * @param tag  标记
     * @param text 日志内容
     */
    public static void debug(String tag, String text) {
        if (isPrintDebugLogPreset) {
            android.util.Log.d(tag, " " + text);
        }
        if (isPrintDebugLogSDPreset && logFilter(tag)) {
            if(tag.equals("step")){
                //storeLog1("debug", tag, text);
            }
        }
    }

    public static void debug(String tag, String format, Object... args) {
        String text = format;
        if (args.length > 0)
            text = String.format(format, args);
        debug(tag, text);
    }

    /**
     * 打印异常信息
     *
     * @param e
     */
    public static void printStackTrace(String tag, Throwable e) {
        if (isPrintErrorLogPreset) {
            android.util.Log.i(tag, " " + e);
        }

        if (isPrintErrorLogSDPreset) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            storeLog("error", tag, sw.toString());
        }
    }

    /**
     * 打印info级别的日记
     *
     * @param tag  标记
     * @param text 日志内容
     */
    public static void info(String tag, String text) {
        if (isPrintInfoLogPreset) {
            android.util.Log.i(tag, " " + text);
        }
        if (isPrintInfoLogSDPreset) {
            storeLog("i", tag, text);
        }

    }

    public static void info(String tag, String format, Object... args) {
        String text = format;
        if (args.length > 0)
            text = String.format(format, args);
        info(tag, text);
    }

    /**
     * 打印warn级别的日记
     *
     * @param tag  标记
     * @param text 日志内容
     */
    public static void warn(String tag, String text) {
        if (isPrintWarnLogPreset) {
            android.util.Log.w(tag, " " + text);
        }
        if (isPrintWarnLogSDPreset) {
            storeLog("w", tag, text);
        }
    }

    public static void warn(String tag, String format, Object... args) {
        String text = format;
        if (args.length > 0)
            text = String.format(format, args);
        warn(tag, text);
    }

    /**
     * 打印warn级别的日记
     *
     * @param tag       标记
     * @param text      日志内容
     * @param throwable 异常信息
     */
    public static void warn(String tag, String text, Throwable throwable) {
        if (isPrintWarnLogPreset) {
            android.util.Log.w(tag, " " + text, throwable);
        }
        if (isPrintWarnLogSDPreset) {
            storeLog("w", tag, text);
        }
    }

    /**
     * 打印error级别的日记
     *
     * @param tag  标记
     * @param text 日志内容
     */
    public static void msgLst(String tag, String text) {
        if (isPrintErrorLogPreset) {
            android.util.Log.e(tag, " " + text);
        }
        if (isPrintErrorLogSDPreset) {
            storeLog("error", tag, text);
        }
    }

    public static void error(String tag, String text) {
        if (isPrintErrorLogPreset) {
            android.util.Log.e(tag, " " + text);
        }
        if (isPrintErrorLogSDPreset) {
            storeLog("error", tag, text);
        }
    }

    public static void error(String tag, String format, Object... args) {
        String text = format;
        if (args.length > 0)
            text = String.format(format, args);
        error(tag, text);
    }

    /**
     * 打印error级别的日记
     *
     * @param tag       标记
     * @param text      日志内容
     * @param throwable 异常信息
     */
    public static void error(String tag, String text, Throwable throwable) {
        if (isPrintErrorLogPreset) {
            android.util.Log.e(tag, " " + text, throwable);
        }
        if (isPrintErrorLogSDPreset && logFilter(tag)) {
            if(tag.equals("step")){
                storeLog1("error", tag, text);
            }
        }

    }

    /**
     * [用于存取错误日志信息] [功能详细描述]
     *
     * @param type log类型
     * @param strModule 类名
     * @param strErrMsg 错误信息
     */
    private static File file;

    public static File getFile() {
        return file;
    }

    public static void storeLog(String type, String strModule, String strErrMsg) {
        PrintWriter out = null;
        FileOutputStream fos = null;
        try {
            file = openFile(LogName);

            if (file == null) {
                return;
            }
            // 输出
            fos = new FileOutputStream(file, true);
            if (fos != null) {
                out = new PrintWriter(fos);
                if (out != null) {
                    String dateNowStr = timeFormat(0);
                    if (type.equals("error")) {
                        out.println(dateNowStr
                            + " Error:>>"
                            + strModule
                            + "<<  "
                            + strErrMsg
                            + '\r');
                    } else if (type.equals("debug")) {
                        out.println(dateNowStr
                            + " Debug:>>"
                            + strModule
                            + "<<  "
                            + strErrMsg
                            + '\r');
                    } else if (type.equals("i")) {
                        out.println(dateNowStr
                            + " Info:>>"
                            + strModule
                            + "<<   "
                            + strErrMsg
                            + '\r');
                    } else if (type.equals("w")) {
                        out.println(dateNowStr
                            + " Warning:>>"
                            + strModule
                            + "<<   "
                            + strErrMsg + '\r');
                    } else if (type.equals("v")) {
                        out.println(dateNowStr
                            + " Verbose:>>"
                            + strModule
                            + "<<   "
                            + strErrMsg
                            + '\r');
                    } else if (type.equals("f")) {
                        out.println(dateNowStr
                            + " File:>>"
                            + strModule
                            + "<<   "
                            + strErrMsg
                            + '\r');
                    }
                }
                out.flush();
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void storeLog1(String type, String strModule, String strErrMsg) {
        PrintWriter out = null;
        FileOutputStream fos = null;
        try {
            file = openFile(LogName1);

            if (file == null) {
                Log.error(TAG,"storeLog1 file null!");
                return;
            }
            // 输出
            fos = new FileOutputStream(file, true);
            if (fos != null) {
                out = new PrintWriter(fos);
                if (out != null) {
                    String dateNowStr = timeFormat(0);
                    if (type.equals("error")) {
                        out.println(dateNowStr
                            + " Error:>>"
                            + strModule
                            + "<<  "
                            + strErrMsg
                            + '\r');
                    } else if (type.equals("debug")) {
                        out.println(dateNowStr
                            + " Debug:>>"
                            + strModule
                            + "<<  "
                            + strErrMsg
                            + '\r');
                    } else if (type.equals("i")) {
                        out.println(dateNowStr
                            + " Info:>>"
                            + strModule
                            + "<<   "
                            + strErrMsg
                            + '\r');
                    } else if (type.equals("w")) {
                        out.println(dateNowStr
                            + " Warning:>>"
                            + strModule
                            + "<<   "
                            + strErrMsg + '\r');
                    } else if (type.equals("v")) {
                        out.println(dateNowStr
                            + " Verbose:>>"
                            + strModule
                            + "<<   "
                            + strErrMsg
                            + '\r');
                    } else if (type.equals("f")) {
                        out.println(dateNowStr
                            + " File:>>"
                            + strModule
                            + "<<   "
                            + strErrMsg
                            + '\r');
                    }
                }
                out.flush();
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String timeFormat(long time) {
        Date dateNow;
        String dateNowStr = null;
        if (time > 0) {
            dateNow = new Date(time);
        } else {
            dateNow = new Date();
        }
        synchronized (dateFormat) {
            dateNowStr = dateFormat.format(dateNow);
        }
        return dateNowStr;
    }

    /**
     * @return
     */
    private static File openFile(String name) {
        String logFileDir = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            logFileDir = Environment.getExternalStorageDirectory() + logPath;
        }

        if (logFileDir == null) {
            return null;
        }

        File fileDir = new File(logFileDir);

        // 判断目录是否已经存在
        if (fileDir != null && !fileDir.exists()) {
            android.util.Log.i("Log", "fileDir is no exists!");
            if (fileDir != null && !fileDir.mkdirs()) {
                return null;
            }
        }

        return new File(logFileDir, name);
    }

    public static void debug(String msg) {
        if (isPrintDebugLogPreset)
            android.util.Log.d(TAG, msg);
    }

    public static void error(String msg) {
        if (isPrintErrorLogPreset)
            android.util.Log.e(TAG, " " + msg);
    }
}
